﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using Client.ServiceReference;

namespace Client
{
    public sealed class ServerConnection
    {
        private static ServerConnection _instance;
        private ServiceReference.WebServicesClient _webServices;
        private Guid _guid;
        public static ServerConnection Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ServerConnection();
                }
                return _instance;
            }
        }

        private ServerConnection()
        {
            _webServices = new WebServicesClient();
            if (Client.Properties.Settings.Default.ClientId.Equals(Guid.Empty) || true)
            {
                _guid = this.RegisterApplication();
                Client.Properties.Settings.Default.ClientId = _guid;
                Client.Properties.Settings.Default.Save();
            }
            else
            {
                _guid = Client.Properties.Settings.Default.ClientId;
                if (!_webServices.Hello(_guid))
                {
                    _guid = this.RegisterApplication();
                    Client.Properties.Settings.Default.ClientId = _guid;
                    Client.Properties.Settings.Default.Save();
                }
            }
        }

        ~ServerConnection()
        {
            _webServices.Close();
        }

        private Guid RegisterApplication()
        {
            string processorName = "";
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor");
            foreach (ManagementObject share in searcher.Get())
            {
                processorName = (string)share["Name"];

            }

            searcher = new ManagementObjectSearcher("SELECT * FROM CIM_OperatingSystem");

            foreach (var item in searcher.Get())
            {
                ulong memory = (ulong)item["TotalVisibleMemorySize"];
            }
            //@TODO: Add info memory;
            Guid id = _webServices.RegisterComputingUnit(processorName, (short)Environment.ProcessorCount);
            return id;
        }

        #region WebSersice methods

        public LibInterface.Compiuting GetComputing()
        {
           return _webServices.GetNextCompiutingData(_guid);
        }

        public ProjectFilesDTO[] GetProjectFiles(int projectId)
        {
           return _webServices.GetProjectFiles(_guid, projectId);
        }

        public ProjectDTOBasic[] GetActiveProjects()
        {
            return _webServices.GetActiveProjects(_guid);
            
        }

        public byte[] GetParamiter(int projectId, int paramiterId, bool isDynamic)
        {
            return _webServices.GetParamiter(_guid, projectId, projectId, isDynamic);
        }
        #endregion

        
    }
}
